//
//  HWGameDetailTradeCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/25.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameDetailTradeCell: UITableViewCell {
    var timeLabel:UILabel!
    var gameImageView:UIImageView!
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var priceLabel:UILabel!
    var model:HWGameDetailTradeModel?{
        didSet {
            timeLabel.text = "上架时间：" + String.timeIntervalChangeToTimeStr(timeInterval: model?.create_time ?? 0, "yyyy-MM-dd")
            gameImageView.sd_setImage(with: URL(string: model?.image ?? ""), completed: nil)
            titleLabel.text = model?.name
            
            contentLabel.text = model?.introduction
            priceLabel.text = "￥" + (model?.money ?? "")
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWGameDetailTradeCell) {
        let reUse:String = "HWGameDetailTradeCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameDetailTradeCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameDetailTradeCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        timeLabel = HWLabel.createLabel(title: "", color: "#333333", font: 12*widthScale)
        contentView.addSubview(timeLabel)
        
        gameImageView = UIImageView()
        gameImageView.contentMode = .scaleAspectFill
        gameImageView.layer.masksToBounds = true
        contentView.addSubview(gameImageView)
        
        titleLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 15*widthScale)
        titleLabel.numberOfLines = 1
        contentView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 14*widthScale)
        contentLabel.numberOfLines = 1
        contentView.addSubview(contentLabel)
        
        priceLabel = HWLabel.createLabel(title: "", color: "#F87706", font: 14*widthScale)
        contentView.addSubview(priceLabel)
    }
    
    func initFrame() -> Void {
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(10*widthScale)
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(timeLabel.snp.bottom).offset(10*widthScale)
            make.height.equalTo(60*widthScale)
            make.width.equalTo(85*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
        timeLabel.setContentHuggingPriority(.required, for: .vertical)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(10*widthScale)
            make.top.equalTo(gameImageView)
            make.right.equalTo(-15*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(10*widthScale)
            make.right.equalTo(priceLabel.snp.left).offset(-10*widthScale)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(titleLabel.snp.bottom)
        }
        
        priceLabel.setContentHuggingPriority(.required, for: .horizontal)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
